import random
import time
import pyautogui

ope_image_path = '../../image/operate\\'


# 定位图片，返回坐标
def find_image(image_path):
    try:
        position = pyautogui.locateCenterOnScreen(image_path, confidence=0.8)
        return position
    except pyautogui.ImageNotFoundException:
        return None


def find_nine_image(image_path):
    try:
        position = pyautogui.locateCenterOnScreen(image_path, confidence=0.9)
        return position
    except pyautogui.ImageNotFoundException:
        return None


def find_exact_image(image_path):
    try:
        position = pyautogui.locateCenterOnScreen(image_path, confidence=1)
        return position
    except pyautogui.ImageNotFoundException:
        return None


def find_vague_image(image_path):
    try:
        position = pyautogui.locateCenterOnScreen(image_path, confidence=0.6)
        return position
    except pyautogui.ImageNotFoundException:
        return None


# 左键位置
def click_position(x, y):
    pyautogui.moveTo(x, y, duration=random_value(0.5, 0.2))
    pyautogui.click(x, y)


# 右键位置
def click_right_position(x, y):
    pyautogui.moveTo(x, y, duration=random_value(0.5, 0.2))
    pyautogui.rightClick(x, y)


# 左键点击图片随机偏移5像素
def click_image(position):
    if position is not None:
        x, y = position
        pyautogui.moveTo(x, y, duration=random_value(0.5, 0.2))
        pyautogui.moveRel(
            -5 + random.randint(0, 5),
            -5 + random.randint(0, 5),
            duration=0.5
        )
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        time.sleep(0.5)


def click_image_path(image_path):
    position = find_image(image_path)
    click_image(position)


# 右键点击图片位置偏移5像素
def click_right_image(position):
    if position is not None:
        x, y = position
        pyautogui.moveTo(x, y, duration=random_value(0.5, 0.2))
        pyautogui.moveRel(
            -5 + random.randint(0, 5),
            -5 + random.randint(0, 5),
            duration=0.5
        )
        pyautogui.mouseDown(button="right")
        pyautogui.mouseUp(button="right")


# 按着键盘点击鼠标
def click_path_with_key(image_path, key):
    position = find_image(image_path)
    if position is not None:
        pyautogui.keyDown(key)
        click_image(position)
        time.sleep(0.5)
        pyautogui.keyUp(key)


def click_position_with_key(position, key):
    if position is not None:
        pyautogui.keyDown(key)
        click_image(position)
        time.sleep(0.5)
        pyautogui.keyUp(key)


def click_exact_position_with_key(position, key):
    position = find_exact_image(position)
    if position is not None:
        pyautogui.keyDown(key)
        click_image(position)
        time.sleep(0.5)
        pyautogui.keyUp(key)


'''
    判定本地是否有红名
        如果有红名，返回True
        如果没有红名，返回False
'''


def is_clear():
    if find_nine_image(ope_image_path + 'red1.png') is None and find_nine_image(ope_image_path + 'red2.png') is None:
        return True
    else:
        print(find_image(ope_image_path + 'red1.png'))
        print(find_image(ope_image_path + 'red2.png'))
        return False


'''
    判定是否在空间站中
        如果在空间站中，返回True
        如果不在空间站中，返回False
'''


def is_in_station():
    if find_image(ope_image_path + 'out_station.png'):
        return True
    else:
        return False


def is_UAV_back():
    if find_image(ope_image_path + 'collection_UAV_done.png'):
        return True
    else:
        return False


def is_jumping():
    if find_image(ope_image_path + 'jumping.png'):
        return True
    else:
        return False


def back_station():
    click_path_with_key(ope_image_path + 'station.png', 'D')


def towards_station():
    click_path_with_key(ope_image_path + 'station.png', 'Q')


# 回收无人机操作
def call_back_UVA():
    pyautogui.keyDown('shift')
    pyautogui.keyDown('r')
    time.sleep(0.5)
    pyautogui.keyUp('r')
    pyautogui.keyUp('shift')
    time.sleep(0.5)


# 释放无人机
def release_UVA_attack():
    pyautogui.keyDown('shift')
    pyautogui.keyDown('f')
    time.sleep(0.5)
    pyautogui.keyUp('f')
    pyautogui.keyUp('shift')
    time.sleep(0.5)


# 保存地点
def save_location(position):
    time.sleep(0.5)
    click_right_image(position)
    time.sleep(0.5)
    click_image_path(ope_image_path + 'save_location.png')
    time.sleep(0.5)
    pyautogui.hotkey('enter')


# 随机生成value+—scop的浮点值
def random_value(value, scop) -> float:
    return random.uniform(value - scop, value + scop)


# 等待随机生成value+—scop的浮点值的时间
def random_sleep(value, scop):
    time.sleep(random_value(value, scop))
